package org.jeecg.modules.clue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.clue.entity.ClueTagAreaConfig;
import org.jeecg.modules.clue.entity.ClueTpCarConfig;
import org.jeecg.modules.clue.entity.ClueUserTagAreaRelate;
import org.jeecg.modules.clue.entity.ClueUserTpCarRelate;
import org.jeecg.modules.clue.mapper.ClueUserTpCarRelateMapper;
import org.jeecg.modules.clue.req.ClueUserTpCarReq;
import org.jeecg.modules.clue.service.IClueTpCarConfigService;
import org.jeecg.modules.clue.service.IClueUserTpCarRelateService;
import org.jeecg.modules.clue.vo.ClueUserTagAreaVo;
import org.jeecg.modules.clue.vo.ClueUserTpCarVo;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @Description: 用户三方平台关联
 * @Author: jeecg-boot
 * @Date: 2025-01-05
 * @Version: V1.0
 */
@Service
@Slf4j
public class ClueUserTpCarRelateServiceImpl extends ServiceImpl<ClueUserTpCarRelateMapper, ClueUserTpCarRelate> implements IClueUserTpCarRelateService {

    @Resource
    ISysUserService sysUserService;
    @Resource
    IClueTpCarConfigService clueTpCarConfigService;

    @Override
    public List<ClueUserTpCarVo> userList(String userId) {

        // 用户地区关联表
        List<ClueUserTpCarRelate> userTpCarRelates = list(new LambdaQueryWrapper<ClueUserTpCarRelate>().eq(ClueUserTpCarRelate::getUserId, userId));
        // 获取可用标签
        List<ClueTpCarConfig> list = clueTpCarConfigService.list(new LambdaQueryWrapper<ClueTpCarConfig>().eq(ClueTpCarConfig::getStatus, "Y").orderByAsc(ClueTpCarConfig::getSort));
        return list.parallelStream().map(clueTpCarConfig -> {
            ClueUserTpCarVo vo = BeanUtil.toBean(clueTpCarConfig, ClueUserTpCarVo.class);
            vo.setTpCarConfigId(clueTpCarConfig.getId());

            // 设置当前
            List<ClueUserTpCarRelate> clueUserTpCarRelates = userTpCarRelates.stream().filter(userTpCarRelate -> userTpCarRelate.getTpCarId().equals(clueTpCarConfig.getId())).collect(Collectors.toList());
            vo.setUserId(userId);
            if (!clueUserTpCarRelates.isEmpty()) {
                ClueUserTpCarRelate carRelate = clueUserTpCarRelates.get(0);
                vo.setUserId(carRelate.getUserId());
                vo.setTpCarId(carRelate.getTpCarId());
                vo.setDelayPushTime(carRelate.getDelayPushTime());
                vo.setTpUsername(carRelate.getTpUsername());
                vo.setTpPassword(carRelate.getTpPassword());
                vo.setTpSecretKey(carRelate.getTpSecretKey());
                vo.setTpSecretKey(carRelate.getTpSecretKey());
                vo.setConfigStatus(carRelate.getStatus());
                vo.setUserCityList(carRelate.getCityList());
                vo.setIzFilter(carRelate.getIzFilter());
            }
            return vo;
        }).collect(Collectors.toList());


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSet(List<ClueUserTpCarReq> clueUserTpCarReqs) {
        log.info("batchSet {}", clueUserTpCarReqs);
        List<ClueUserTpCarRelate> relateList = clueUserTpCarReqs.parallelStream().filter(clueUserTpCarReq -> StringUtils.isNotBlank(clueUserTpCarReq.getTpCarId()))
                .map(clueUserTpCarReq -> {
                    ClueUserTpCarRelate carRelate = BeanUtil.toBean(clueUserTpCarReq, ClueUserTpCarRelate.class);
                    carRelate.setCityList(clueUserTpCarReq.getUserCityList());
                    carRelate.setStatus(clueUserTpCarReq.getConfigStatus());
                    return carRelate;
                }).collect(Collectors.toList());

        // 先删除
        remove(new LambdaQueryWrapper<ClueUserTpCarRelate>().eq(ClueUserTpCarRelate::getUserId, clueUserTpCarReqs.get(0).getUserId()));
        // 新增
        saveBatch(relateList);

    }

    @Override
    public List<ClueUserTpCarVo> userListByUsername(String username) {
        SysUser sysUser = sysUserService.getUserByName(username);
        if (null == sysUser) {
            throw new RuntimeException("用户不存在");
        }
        return userList(sysUser.getId());
    }

    @Override
    public List<ClueTpCarConfig> userSelectPart() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 用户地区关联表
        List<ClueUserTpCarRelate> userTpCarRelates = list(new LambdaQueryWrapper<ClueUserTpCarRelate>()
                .eq(ClueUserTpCarRelate::getIzFilter, "Y")
                .eq(ClueUserTpCarRelate::getUserId, sysUser.getId()));
        if (userTpCarRelates.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        // 获取可用标签
        List<ClueTpCarConfig> list = clueTpCarConfigService.list(new LambdaQueryWrapper<ClueTpCarConfig>()
                        .in(ClueTpCarConfig::getId, userTpCarRelates.parallelStream().map(ClueUserTpCarRelate::getTpCarId).collect(Collectors.toList()))
                .eq(ClueTpCarConfig::getStatus, "Y").orderByAsc(ClueTpCarConfig::getSort));
        return list;

    }
}
